//
//  MovieLibraryVC.m
//  彩虹儿歌
//
//  Created by HelloWorld_L on 15/7/2.
//  Copyright (c) 2015年 HelloBaby. All rights reserved.
//

#import "MovieLibraryVC.h"
#import "RequestManager.h"
#import "LVCModel.h"
#import "LVCollectionViewCell.h"
#import "MacroHeader.h"
#import "LibraryViewController.h"
@interface MovieLibraryVC ()<RequestDelegate>

@property (nonatomic, retain) RequestManager *request;

@property (nonatomic, retain) NSMutableArray *dataSourceArr;

@end

@implementation MovieLibraryVC

- (void)dealloc
{
    [_request.delegate release];
    [_dataSourceArr release];
    [super dealloc];
}

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *backgroundImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiaohua01"]];
    [self.view addSubview:backgroundImgV];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.dataSourceArr = [NSMutableArray array];
    
    [self requestData];
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.itemSize = CGSizeMake( (SCREEN_WIDTH - 40) / 3, (SCREEN_WIDTH - 40) / 3 + 30);
    flowLayout.footerReferenceSize = CGSizeMake(0, 50);
    
    [self.collectionView registerClass:[LVCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
//    [flowLayout release];
//    [self.collectionView release];
    
    // Do any additional setup after loading the view.
}

#pragma mark - 网络请求数据

- (void)requestData
{
    self.request = [[RequestManager alloc] initWithRequestURL:@"http://app.beva.com/box-packageItemData/p46/chbeva/kwerge--v2.0.json" requestModel:GET parameter:nil];
    self.request.delegate = self;
    [self.request startRequest];
    [self.request release];
}

- (void)requestDidFinishWithData:(NSData *)data
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSArray *arr = dic[@"data"];
    for (NSDictionary *modelDic in arr) {
        LVCModel *model = [[LVCModel alloc] init];
        [model setValuesForKeysWithDictionary:modelDic];
        [self.dataSourceArr addObject:model];
        [model release];
    }
    NSLog(@"%ld",self.dataSourceArr.count);
    [self.collectionView reloadData];
}

- (void)requestError:(NSError *)error
{
    NSLog(@"%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataSourceArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LVCModel *model = self.dataSourceArr[indexPath.item];
    LVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LibraryViewController *libraryVC = [[LibraryViewController alloc] init];
    libraryVC.plistId = [(LVCModel *)self.dataSourceArr[indexPath.item] ID];
    libraryVC.naviBarItemTitle = [(LVCModel *)self.dataSourceArr[indexPath.item] name];
    [self.navigationController pushViewController:libraryVC animated:YES];
    [libraryVC release];
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
